package com.peas.platform.module.icims.base.mdm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.mdm.domain.FileDomain;
import com.peas.platform.module.icims.base.mdm.dto.FileDto;
import com.peas.platform.module.icims.base.mdm.enums.AttachmentType;
import com.peas.platform.module.icims.base.mdm.service.FileService;
import com.peas.platform.module.icims.base.sys.service.AttachmentService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
public class FileServiceImpl extends BaseServiceImpl<FileDto> implements FileService {

    @Resource
    private FileDomain fileDomain;
    @Resource
    private AttachmentService attachmentService;

    @Override
    public BaseDomain<FileDto, ? extends BasePo> getDomain() {
        return fileDomain;
    }

    @Override
    public List<FileDto> findFileListByDir(OrderItem order, FileDto condition) {
        return fileDomain.findFileListByDir(order, condition);
    }

    @Transactional
    @Override
    public boolean moves(List<Long> ids, Long parentId) {
        Assert.notNull(parentId, "请选择目标文件夹");
        List<FileDto> list = this.listByIds(ids);
        CollUtil.forEach(list, (CollUtil.Consumer<FileDto>) (value, index) -> {
            value.setParentId(parentId);
            this.updateById(value);
        });
        return true;
    }

    @SneakyThrows
    @Override
    public void zipFiles(List<FileDto> list, ZipOutputStream zipOutputStream, String parentFileName) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        for (FileDto dto : list) {
            if (StrUtil.equals(AttachmentType.directory.getKey(), dto.getAttachmentType())) {
                // ZipEntry zipEntry = new ZipEntry(parentFileName + "/" + dto.getFileName());
                // zipOutputStream.putNextEntry(zipEntry);
                // 是目录就需要遍历子目录
                FileDto c = new FileDto();
                c.setParentId(dto.getId());
                List<FileDto> subs = this.list(c);
                String str = StrUtil.isBlank(parentFileName) ? dto.getFileName() : parentFileName + "/" + dto.getFileName();
                zipFiles(subs, zipOutputStream, str);
            }
            if (StrUtil.equals(AttachmentType.file.getKey(), dto.getAttachmentType())) {
                String fileName = StrUtil.isBlank(parentFileName) ? dto.getFileName() : parentFileName + "/" + dto.getFileName();
                ZipEntry zipEntry = new ZipEntry(fileName);
                zipOutputStream.putNextEntry(zipEntry);
                InputStream in = attachmentService.getObject(dto.getAttachmentId());
                int len;
                byte[] buf = new byte[8 * 1024];
                while ((len = in.read(buf)) != -1) {
                    zipOutputStream.write(buf, 0, len);
                }
            }
        }
    }

}
